package cc.z8g.browser.core

import android.app.ActivityOptions
import android.content.ClipData
import android.content.Intent
import androidx.core.content.ContextCompat.startActivity
import androidx.fragment.app.FragmentActivity
import cc.z8g.browser.IncognitoBrowserActivity
import cc.z8g.browser.R
import cc.z8g.browser.core.download.DownloadRequest
import cc.z8g.browser.core.config.ExitCleanup
import cc.z8g.browser.SimpleSettingsActivity
import cc.z8g.browser.core.download.DownloadHandler
import cc.z8g.browser.utils.IntentUtils
import cc.z8g.browser.utils.SystemServiceManager
import cc.z8g.browser.utils.MiscUtils


/**
 * 菜单项等功能的实现
 */
class BrowserNavigator(private val fragmentActivity: FragmentActivity) {

    /** 打开设置页面 */
    fun openSettings() {
        val activity = fragmentActivity
        val options = ActivityOptions.makeCustomAnimation(activity, 0, 0)
        val intent = Intent(activity, SimpleSettingsActivity::class.java)
        startActivity(activity, intent, options.toBundle())
    }

    /** 分享页面链接 */
    fun sharePage(url: String, title: String?) {
        IntentUtils.shareUrl(fragmentActivity, url, title)
    }

    /** 复制链接到剪贴板 */
    fun copyPageLink(url: String) {
        SystemServiceManager.clipboardManager.setPrimaryClip(ClipData.newPlainText("URL", url)) // copyToClipboard
        MiscUtils.showToast(fragmentActivity, R.string.message_link_copied)
    }

    /** 关闭浏览器，终止会话 */
    fun closeBrowser() {
        ExitCleanup.cleanUp(fragmentActivity)
        fragmentActivity.finish()
    }

    /** Download the file provided by the [downloadRequest]. */
    fun download(downloadRequest: DownloadRequest) {
        DownloadHandler.download(fragmentActivity, downloadRequest)
    }

    /** Move the browser to the background without terminating the session. */
    fun backgroundBrowser() {
        fragmentActivity.moveTaskToBack(true)
    }

    /** 用url打开匿名模式 */
    fun launchIncognitoWindow(url: String?) {
        IntentUtils.launchActivity(fragmentActivity, IncognitoBrowserActivity::class.java, url);
    }
}
